package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.custom.RoundedImageView;
import com.luck.easyfoodbatch.entity.User;
import com.luck.easyfoodbatch.mvp.mine.ShoperInfoModel;
import com.luck.easyfoodbatch.mvp.mine.ShoperInfoPresenter;
import com.luck.easyfoodbatch.mvp.mine.ShoperInfoView;
import com.luck.easyfoodbatch.mvp.model.ShoperModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopInfoDetailActivity extends BaseActivity implements ShoperInfoView {
    private final String TAG = "ShopInfoT2Activity";
    @BindView(R.id.rb_id)
    RadioButton rbId;
    @BindView(R.id.rb_dc)
    RadioButton rbDc;
    @BindView(R.id.rgbs)
    RadioGroup rgbs;
    @BindView(R.id.et_real_name)
    TextView etRealName;
    @BindView(R.id.ll_real_name)
    LinearLayout llRealName;
    @BindView(R.id.et_phone)
    TextView etPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.shop_avatar)
    RoundedImageView shopAvatar;
    @BindView(R.id.rl_avatar)
    RelativeLayout rlAvatar;
    @BindView(R.id.et_shop_name)
    TextView etShopName;
    @BindView(R.id.ll_shop_name)
    LinearLayout llShopName;
    @BindView(R.id.et_shop_address)
    TextView etShopAddress;
    @BindView(R.id.ll_shop_address)
    LinearLayout llShopAddress;
    @BindView(R.id.identity_avatar)
    RoundedImageView identityAvatar;
    @BindView(R.id.rl_identity)
    RelativeLayout rlIdentity;
    @BindView(R.id.et_main_brand)
    TextView etMainBrand;
    @BindView(R.id.ll_main_brand)
    LinearLayout llMainBrand;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    @BindView(R.id.tv_business)
    TextView tv_business;
    @BindView(R.id.rl_business)
    RelativeLayout rl_business;
    @BindView(R.id.riv_business)
    RoundedImageView riv_business;
    @BindView(R.id.tv_brand_tag)
    TextView tv_brand_tag;
    @BindView(R.id.ll_bzj)
    LinearLayout ll_bzj;
    private String product_type = "1";
    private ShoperInfoModel infoModel;
    private ShoperModel.DataBean data;
    private User user;
    // 1是客服权限只能访问我的商品和消息模块（商品和聊天）
    // 2是运营，只能访问我的商品模块（管理商品
    // 3是财务，只能访问商家信息模块（支付） ,也有可能同时兼备多个权限，（1,2就代表既是客服也是运营）
    private String role = "0";
    private List<Integer> roles = new ArrayList<>();// 权限标识

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopinfo_detail, "商家信息", "编辑", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        infoModel = new ShoperInfoPresenter(this, mContext);
        infoModel.shoperInfo(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
        if (user != null) {
            role = user.getRole();
            roles.clear();
            String[] split = role.split(",");
            for (String code : split) {
                if (!Tools.isNull(code)) {
                    roles.add(Integer.parseInt(code));
                }
            }
            // 2为已申请未交金3为已申请已交金4为已通过5为未通过6.补缴保证金
            int is_shoper = user.getIs_shoper();
            if (is_shoper == 2 || is_shoper == 3 || is_shoper == 5) {
                ll_bzj.setVisibility(View.GONE);
            } else if (is_shoper == 4) {
                // 为已通过
                if (is_shoper == 3) {
                    // 已交保证金
                    ll_bzj.setVisibility(View.GONE);
                } else {
                    // 未交保证金
                    ll_bzj.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void rightClick() {
        super.rightClick();
        if (roles.contains(2) || roles.contains(3)) {
            T.showAnimToast(mContext, "抱歉，您没有操作权限~");
        } else {
            if (data != null) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", data);
                bundle.putInt("type", 1);// 编辑模式
                intent.putExtras(bundle);
                intent.setClass(mContext, ShopInfoT1Activity.class);
                startActivityForResult(intent, 200);
            }
        }
    }


    @Override
    public void ShoperInfoSuccess(ShoperModel model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                data = model.getData();
                etRealName.setText(data.getName());
                etPhone.setText(data.getPhone());
                etShopName.setText(data.getShop_name());
                etShopAddress.setText(data.getShop_address());
                etMainBrand.setText(data.getBrand());
                String shop_avatar = data.getShop_avatar();
                if (!Tools.isNull(shop_avatar)) {
                    Glide.with(getApplicationContext())
                            .load(HttpUtil.IMG_BASE_URL + shop_avatar)
                            .centerCrop()
                            .placeholder(R.mipmap.camera)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(shopAvatar);
                } else {
                    shopAvatar.setImageResource(R.mipmap.camera);
                }
                String id_card = data.getId_card();
                if (!Tools.isNull(id_card)) {
                    Glide.with(getApplicationContext())
                            .load(HttpUtil.IMG_BASE_URL + id_card)
                            .centerCrop()
                            .placeholder(R.mipmap.camera)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(identityAvatar);
                } else {
                    identityAvatar.setImageResource(R.mipmap.camera);
                }

                int type = data.getProduct_type();
                // 保存商家商品类型

                user.setProduct_type(type);

                App.getInstance().saveObject(user, HttpUtil.USER_KEY);
                User u = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                Log.i("商品类型2---------<", u.getProduct_type() + "");

                switch (type) {
                    case 1:
                        product_type = "1";
                        tv_brand_tag.setText("主营品牌");
                        rbId.setChecked(true);
                        break;
                    case 2:
                        product_type = "2";
                        tv_brand_tag.setText("代理品牌");
                        rbDc.setChecked(true);
                        break;
                }

            } else {

            }
        }
    }

    @Override
    public void httpStatusError() {
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    if (infoModel == null) {
                        infoModel = new ShoperInfoPresenter(ShopInfoDetailActivity.this, mContext);
                    }
                    infoModel.shoperInfo(0);
                    break;
                case 300:
                    User user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
                    if (user != null) {
                        // 2为已申请未交金3为已申请已交金4为已通过5为未通过6.补缴保证金
                        int is_shoper = user.getIs_shoper();
                        if (is_shoper == 2) {
                            // 是商家 但未交保证金
                            ll_bzj.setVisibility(View.VISIBLE);
                        } else if (is_shoper == 3) {
                            ll_bzj.setVisibility(View.GONE);
                        }
                    }
                    break;
            }
        }
    }

    @OnClick(R.id.btn_exchange)
    public void onClick() {
        Intent intent = new Intent();
        intent.setClass(mContext, PaymentBondActivity.class);
        startActivityForResult(intent, 300);
    }
}
